Using Modules 1: Core Modules

Firat Atalay
4 min readMay 28

Okay, so let’s now write our very first Node code here inside a file and all I’m gonna do is to create a hello variable. And then putting the classic “Hello world” in there. So “Hello world” has been traditionally used when starting a new programming language and that’s kind of what we’re doing here. Okay, and now I’m logging to the console this hello variable, so very very very simple stuff.

Give it a save and let’s actually now run this file. So, in normal JavaScript in a browser, we would now include this JavaScript file into some HTML file and then open up that HTML file in a browser, right? But here with Node, we don’t need to do anything like that.

All we do is to use our Node command. But now, we’re not gonna hit ‘Enter’ right away, because that would take us back to the REPL but instead we want to run this file. And so all we have to do is to write Node and then the name of the file, so index.js, hit ‘Enter’ and indeed, here we have our log “Hello world.” So congratulations, you just ran your very first Node script.

Now, that isn’t really all that useful, is it? So let’s do something a bit more advanced. And remember how I said right in the first lecture that with Node.js, we can do all kinds of amazing things that we cannot do with JavaScript in the browser like for example reading files from the file system, right? Now in order to do that, we need to use a Node module. So Node.js is really built around this concept of modules where all kinds of additional functionality are stored in a module. And in the case for reading files, that is inside the FS module.

So how do we open up these modules, or how can we actually use them?

Well, we do require them into our code and then store the result of the requiring function in a variable. So that sounds a bit complicated, so let’s simply do it. So we will call FS to the result of requiring the FS module. And FS here stands for file system. So by using this module here, we will get access to functions for reading data and writing data right to the file system.

So again, calling this function here with this built-in FS module name will then return an object in which there are lots of functions that we can use. And restore that object right into the FS variable that we can then later use.

We are going to use it in the next lecture, but for now I want to quickly take a look at the Node documentation with you, so that in case you need some other module for yourself later, you always know where to look up some information about it. Okay, so the Node documentations are something really, really important for every Node developer to know about. So, what we do is to go to nodejs.org and then hit the documentations tab here and then select the Node version that you’re using here on the left side. And so that is the documentation. Here on the left side, you have all kinds of different modules.

For example, you have the REPL as well, so the REPL is what we used in the last lecture and if you need to know a bit more about it, well you can come down here and read whatever you need to know. For example, the exit that I talked about is documented here and well, all kinds of stuff. Okay, you can also actually use the REPL inside your code, which doesn’t make much sense but is actually possible. So again all the modules are listed here on the left side and the one that we just implemented is the file system. Okay, and so down here you have all this different stuff that for now will not make much sense to you, okay, but that’s no problem at all. For now just know that in case you need anything, it is always available in the documentation for you.

So for example, in the next lecture, we will read a file from the disk to our code. And we’re gonna do that using the read file sync. And so here you have all the information about that specific function.

Okay, and so in the next lecture, that is actually what we’re gonna do. And in this one, you learned how to write your own very first Node code inside of a new JavaScript file, and then run that file in the terminal. Then we also required the file system module and so, again, in the next lecture, we will finally use it and import some files.

See you then.